package wang;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 简单模式生产者
 */
public class ProducerRabbitMQ {

    public static void main(String[] args) throws IOException, TimeoutException {


        // 1.创建连接
        ConnectionFactory cf = new ConnectionFactory();

        // 2.配置连接参数
        cf.setHost("47.111.252.79");
        cf.setPort(5672);
        cf.setVirtualHost("/wang");
        cf.setUsername("wang");
        cf.setPassword("wang");

        // 3.创建连接
        Connection connection = cf.newConnection();

        // 4. 创建channel
        Channel channel = connection.createChannel();

        // 5. 创建队列
        /*
        String queue, 队列名称
        boolean durable, 是否持久化
        boolean exclusive,是否独占（只能一个消费者监听）；关闭连接自动删除
        boolean autoDelete, 是否自动删除，没有消费者时自动删除
        Map<String, Object> arguments 参数
         */
        channel.queueDeclare("hello-world", true, false, false, null);

        // 6.发送信息
        /*
        String exchange,交换机名称，默认:”“
        String routingKey, 路由名称 简单模式路由名称为队列名称
        BasicProperties props,
        byte[] body 消息体
         */
        for (int i = 0; i < 20; i++) {
            String mess = "发送消息中...." + i;
            channel.basicPublish("", "hello-world",null, mess.getBytes());
        }


        channel.close();
        connection.close();
    }
}
